using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Acms.Bll
{
    #region Site
    public partial class Site : GeneratedSite
    {
        public Site() : base()
        {
        }

        internal Site(Hashtable table):base(table)
        {
        }
    }

    public abstract class GeneratedSite : BaseItem
    {
        protected GeneratedSite()
        {
        }

        protected GeneratedSite(Hashtable table)
        {
            if (table[Acms.DalInterface.SiteFields.Id] is int)
            {
                id = (int)table[Acms.DalInterface.SiteFields.Id];
            }
            if (table[Acms.DalInterface.SiteFields.Name] is string)
            {
                name = (string)table[Acms.DalInterface.SiteFields.Name];
            }
            if (table[Acms.DalInterface.SiteFields.RootUrl] is string)
            {
                rootUrl = (string)table[Acms.DalInterface.SiteFields.RootUrl];
            }
            if (table[Acms.DalInterface.SiteFields.Created] is DateTime)
            {
                created = (DateTime)table[Acms.DalInterface.SiteFields.Created];
            }
            if (table[Acms.DalInterface.SiteFields.Cid] is int)
            {
                cid = (int)table[Acms.DalInterface.SiteFields.Cid];
            }
            AcceptChanges();
        }

        public static Site GetById(int id)
        {
            Acms.DalInterface.ISite tmp = Acms.DalFactory.Site.CreateInstance();
            Hashtable tmpTable = tmp.GetById(id);
            if (tmpTable != null)
            {
                return new Site(tmpTable);
            }
            else
            {
                return null;
            }
        }

        public static Site GetByRootUrl(string rootUrl)
        {
            Acms.DalInterface.ISite tmp = Acms.DalFactory.Site.CreateInstance();
            Hashtable tmpTable = tmp.GetByRootUrl(rootUrl);
            if (tmpTable != null)
            {
                return new Site(tmpTable);
            }
            else
            {
                return null;
            }
        }

        #region Fields
        public enum Fields
        {
            Id,
            Name,
            RootUrl
        }

        private int id;
        public virtual int Id
        {
            get{ return id;}
        }

        private string name;
        public virtual string Name
        {
            get { return name; }
            set 
            {
                if (name != value)
                {
                    string old = name;
                    name = value;
                    SetChanged(Fields.Name, old, value);
                }
            }
        }

        private string rootUrl;
        public virtual string RootUrl
        {
            get { return rootUrl; }
            set
            {
                if (rootUrl != value)
                {
                    string old = rootUrl;
                    rootUrl = value;
                    SetChanged(Fields.RootUrl, old, value);
                }
            }
        }

        private DateTime created;
        public virtual DateTime Created
        {
            get { return created; }
        }

        private int cid;
        protected virtual int Cid
        {
            get { return cid; }
        }
        #endregion

        public override bool SaveChanges()
        {
            if (!base.IsDirty && !base.IsDeleted)
            {
                return false;
            }
            else if (base.IsDeleted)
            {
                Acms.DalInterface.ISite dal = Acms.DalFactory.Site.CreateInstance();
                dal.Delete(id);
                AcceptChanges();
                return true;
            }
            else
            {
                Acms.DalInterface.ISite dal = Acms.DalFactory.Site.CreateInstance();
                dal.SaveChanges(id, name, rootUrl, cid, ref id, ref created, ref cid);
                AcceptChanges();

                return true;
            }
        }
    }

    public partial class SiteCollection : GeneratedSiteCollection
    {
        public SiteCollection()
        {
        }

        public SiteCollection(Hashtable[] table) : base(table)
        {
        }
    }

    public abstract class GeneratedSiteCollection : BaseCollection<Site>
    {
        protected GeneratedSiteCollection()
        {
        }

        protected GeneratedSiteCollection(Hashtable[] table)
        {
            foreach (Hashtable t in table)
            {
                base.Add(new Site(t));
            }
        }

        public static SiteCollection GetAll()
        {
            Acms.DalInterface.ISite tmp = Acms.DalFactory.Site.CreateInstance();
            return new SiteCollection(tmp.GetAll());
        }


        public void UpdateItem(Site item)
        {
            foreach (Site i in this)
            {
                if (i.Id == item.Id)
                {
                    i.Name = item.Name;
                    i.RootUrl = item.RootUrl;
                }
            }
        }

        public void UpdateItem(int id, string name, string rootUrl)
        {
            foreach (Site i in this)
            {
                if (i.Id == id)
                {
                    i.Name = name;
                    i.RootUrl = rootUrl;
                }
            }
        }

        public Site Add(string name, string rootUrl)
        {
            Site item = new Site();
            item.Name = name;
            item.RootUrl = rootUrl;
            this.Add(item);
            return item;
        }

        /*public void Add(Site item)
        {
            base.Add(item);
        }

        public Site Add()
        {
            Site item = new Site();
            base.Add(item);
            return item;
        }*/
    }
    #endregion

    #region Page
    public partial class Page : GeneratedPage
    {
        public Page()
            : base()
        {
        }

        internal Page(Hashtable table)
            : base(table)
        {
        }

        internal Page(Hashtable table, PageCollection parent)
            : base(table)
        {
        }
    }

    public abstract class GeneratedPage : BaseItem
    {
        protected GeneratedPage()
        {
        }

        protected GeneratedPage(Hashtable table)
        {
            ReadHashTable(table);
        }

        protected GeneratedPage(Hashtable table, PageCollection collection)
        {
            ReadHashTable(table);
            _collection = collection;
        }

        protected virtual void ReadHashTable(Hashtable table)
        {
            if (table[Acms.DalInterface.PageFields.Id] is int)
            {
                _id = (int)table[Acms.DalInterface.PageFields.Id];
            }
            if (table[Acms.DalInterface.PageFields.VersionId] is int)
            {
                _versionId = (int)table[Acms.DalInterface.PageFields.VersionId];
            }
            if (table[Acms.DalInterface.PageFields.SiteId] is int)
            {
                _siteId = (int)table[Acms.DalInterface.PageFields.SiteId];
            }
            if (table[Acms.DalInterface.PageFields.ParentId] is int)
            {
                _parentId = table[Acms.DalInterface.PageFields.ParentId] as int?;
            }
            if (table[Acms.DalInterface.PageFields.Name] is string)
            {
                _name = (string)table[Acms.DalInterface.PageFields.Name];
            }
            if (table[Acms.DalInterface.PageFields.DisplayName] is string)
            {
                _displayName = (string)table[Acms.DalInterface.PageFields.DisplayName];
            }

            AcceptChanges();
        }

        private PageCollection _collection;
        public PageCollection Collection
        {
            get
            {
                return _collection;
            }
        }

        #region Fields
        public enum Fields
        {
            Id,
            VersionId,
            SiteId,
            ParentId,
            Name,
            DisplayName
        }

        private int _id;
        public virtual int Id
        {
            get { return _id; }
        }

        private int _versionId;
        public virtual int VersionId
        {
            get { return _versionId; }
        }

        private int _siteId;
        public virtual int SiteId
        {
            get { return _siteId; }
            set
            {
                if (_siteId != value)
                {
                    int old = _siteId;
                    _siteId = value;
                    SetChanged(Fields.SiteId, old, value);
                }
            }
        }

        private int? _parentId;
        public virtual int? ParentId
        {
            get { return _parentId; }
            set
            {
                if (_parentId != value)
                {
                    int? old = _parentId;
                    _parentId = value;
                    SetChanged(Fields.ParentId, old, value);
                }
            }
        }

        private string _name;
        public virtual string Name
        {
            get { return _name; }
            set
            {
                if (_name != value)
                {
                    string old = _name;
                    _name = value;
                    SetChanged(Fields.Name, old, value);
                }
            }
        }

        private string _displayName;
        public virtual string DisplayName
        {
            get { return _displayName; }
            set
            {
                if (_displayName != value)
                {
                    string old = _displayName;
                    _displayName = value;
                    SetChanged(Fields.DisplayName, old, value);
                }
            }
        }
        #endregion

        public override bool SaveChanges()
        {
            if (!base.IsDirty && !base.IsDeleted)
            {
                return false;
            }
            else if (base.IsDeleted)
            {
                Acms.DalInterface.IPage dal = Acms.DalFactory.Page.CreateInstance();
                dal.Delete(_id);
                AcceptChanges();
                return true;
            }
            else
            {
                Acms.DalInterface.IPage dal = Acms.DalFactory.Page.CreateInstance();
                dal.SaveChanges(_id, _versionId, _siteId, _parentId, _name, _displayName, ref _id, ref _versionId);
                AcceptChanges();

                return true;
            }
        }
    }

    public partial class PageCollection : GeneratedPageCollection
    {
        public PageCollection()
        {
        }

        public PageCollection(Hashtable[] table)
            : base(table)
        {
        }
    }

    public abstract class GeneratedPageCollection : BaseCollection<Page>
    {
        protected GeneratedPageCollection()
        {
        }

        protected GeneratedPageCollection(Hashtable[] table)
        {
            foreach (Hashtable t in table)
            {
                base.Add(new Page(t, this as PageCollection));
            }
        }

        public static PageCollection GetByParentId(int parentId)
        {
            Acms.DalInterface.IPage dal = Acms.DalFactory.Page.CreateInstance();
            return new PageCollection(dal.GetByParentId(parentId));
        }

        public static PageCollection GetRoot(int siteId)
        {
            Acms.DalInterface.IPage dal = Acms.DalFactory.Page.CreateInstance();
            return new PageCollection(dal.GetRoot(siteId));
        }


/*        public void UpdateItem(Page item)
        {
            foreach (Page i in this)
            {
                if (i.Id == item.Id)
                {
                    i.Name = item.Name;
                    i.RootUrl = item.RootUrl;
                }
            }
        }

        public void UpdateItem(int id, string name, string rootUrl)
        {
            foreach (Page i in this)
            {
                if (i.Id == id)
                {
                    i.Name = name;
                    i.RootUrl = rootUrl;
                }
            }
        }
*/
/*        public Page Add(string name, string rootUrl)
        {
            Page item = new Page();
            item.Name = name;
            item.RootUrl = rootUrl;
            this.Add(item);
            return item;
        }

        public void Add(Page item)
        {
            base.Add(item);
        }

        public Page Add()
        {
            Page item = new Page();
            base.Add(item);
            return item;
        }
*/
    }
    #endregion
}
